package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.wallet.dao.WalletInfoDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.model.entity.WalletAccountInfoDoo;
import cn.wp.ewallet.wallet.model.entity.WalletInfoDo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import cn.wp.common.core.dao.JdbcSupportTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 钱包-数据访问层实现
 * Created by zhangyongli on 2020/01/08
 */
@Repository
public class WalletInfoDaoImpl implements WalletInfoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public void insertWallefInfo(WalletInfoDo walletInfoDo) {
        jdbcSupportTemplate.insert(walletInfoDo);
    }

    @Override
    public void updateWallInfo(WalletInfoDo walletInfoDo) {
        jdbcSupportTemplate.update(walletInfoDo);
    }

    @Override
    public WalletInfoDo selectWalletInfo(String address) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from wallet_info");
        sqlString.add("where wallet_address = ?");

        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[]{address}, WalletInfoDo.class);
    }

    @Override
    public List<WalletAccountInfoDoo> selectAllWalletUsdtAccount(String address) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select w.*, a.account_id, a.account_name, a.accout_type, a.money_amount");
        sqlString.add("    , a.money_type_id, m.money_type_unit, d.contract_address");
        sqlString.add("from wallet_info w, account_info a, money_type_info m, digital_money_info d");
        sqlString.add("where w.wallet_address = a.wallet_address");
        sqlString.add("    and a.money_type_id = m.money_type_id");
        sqlString.add("    and m.money_type_id = d.money_type_id");
        sqlString.add("    and a.accout_type = ?");
        sqlString.add("    and a.money_type_id = ?");
        List<String> params = new ArrayList<>();
        params.add(EwalletWalletConstant.ACCOUNT_TYPE_AVAILABLE);
        params.add(EwalletWalletConstant.MONEY_TYPE_USDT);
        if (StringUtils.isNotEmpty(address)) {
            sqlString.add("    and a.wallet_address = ?");
            params.add(address);
        }

        return jdbcSupportTemplate.selectList(sqlString.build(), params.toArray(), WalletAccountInfoDoo.class);
    }

    @Override
    public List<WalletInfoDo> getAllByWalletType() {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select w.* from wallet_info w where w.wallet_type = ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{EwalletWalletConstant.WALLET_TYPE_PRIVATE},WalletInfoDo.class);
    }

    @Override
    public List<WalletAccountInfoDoo> selectAllWalletAccountByAddress(String address) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select w.*, a.account_id, a.account_name, a.accout_type, a.money_amount");
        sqlString.add("    , a.money_type_id, m.money_type_unit, d.contract_address");
        sqlString.add("from wallet_info w, account_info a, money_type_info m, digital_money_info d");
        sqlString.add("where w.wallet_address = a.wallet_address");
        sqlString.add("    and a.money_type_id = m.money_type_id");
        sqlString.add("    and m.money_type_id = d.money_type_id");
        sqlString.add("    and a.wallet_address = ?");
        List<String> params = new ArrayList<>();
        params.add(address);

        return jdbcSupportTemplate.selectList(sqlString.build(), params.toArray(), WalletAccountInfoDoo.class);
    }
}